package game.tileMap.control
{
	import com.greensock.TweenLite;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	
	import game.tileMap.GameScene;
	import game.tileMap.model.BattleModel;
	import game.tileMap.model.Battler;
	import game.tileMap.model.GameMapModel;
	import game.tileMap.model.vo.Unit;
	import game.tileMap.view.TileMapView;
	import game.tileMap.view.UnitView;
	
	import mx.core.UIComponent;
	
	

	public class BattleController extends EventDispatcher
	{
		private var map:GameMapModel
		private var battleModel:BattleModel;
		private var scene:GameScene;
		//private var master:Master
		private var mapView:TileMapView
		public function BattleController(scene:GameScene)
		{
			
			this.scene=scene
			map=scene.map;
			battleModel=scene.battleModel;
			mapView=scene.mapView;
			init()
		}
		
		
		
//		private function get battleScene():BattleScene{
//			return view.battleScene as BattleScene;
//		}
//		
//		
		
//		private function get cpu():CPUMaster{
//			return appProxy.cpuMaster ;
//		}
		
		private function init():void{
			
			map.addEventListener(GameMapModel.START_FIGHT,startFight);
			battleModel.addEventListener(BattleModel.DISPLAY_ACTION,playAction);
			
			map.addEventListener(GameMapModel.NEW_TURN,startNewTurn);
			//mapView.addEventListener(TileMapView.MOVE_END,moveEnd);
			//warScene.cmdBox.addEventListener(MouseEvent.CLICK,selectAct);
			//warScene.battleScene.addEventListener(MouseEvent.CLICK,clickBattleScene);
			//mapView.tileBox.addEventListener(MouseEvent.CLICK, clickTile);
			//warScene.battleScene.battleModel=mapView.map.battleModel
//			battleScene.visible=false;
//			master=new Master(warScene.mapView);
//			
//			
//			warScene.mapView.makePlayer();
		}
		
		
		//开始新回合
		private function startNewTurn(event:Event):void{
//			appProxy.cpuMaster.act2();
//			
			/*if (map.turnSide==0){
				mapView.resetTurn();
			}else{
				master.startAct();
			}*/
			
		}
		
		
		

		
		
		private function clickTile(event:MouseEvent):void{
			
			
		}
		
		private function moveEnd(event:Event):void{
			
			
			
			
			/*if (map.turnSide>0){
				if (mapView.curUnit.action.isAttack){
					//进入攻击界面
					startFight();
				}else{
					mapView.curUnitSp.endAct();
					if (map.checkEndTurn()){
						map.nextTroop();
					}else{
						TweenLite.delayedCall(1,master.act2);
					}
				}
				
				
			}else if (map.turnSide==0){
				showAction();
			}*/
			
			
		}
		
		private function showAction():void{
			
		}
		
		private function selectAct(event:Event):void{
			
			
		}
		
		private function startFight(evt:Event=null):void{
			if (map.troops[1]!=null){
				trace("----------战斗！！！-----------------")
				mapView.party.ctrl=false
				battleModel.start();
				
			}
			
//			var battler:Battler=battleModel.activeBattler;
//			
			
//			var unitView:UnitView=mapView.getUnit(battler);
//			unitView.playAttack(2);
			
			//map.startBattle(battler,battler.action.actTargets[0] as Battler);
			/*warScene.battleScene.visible=true
			mapView.clearRange();
			warScene.battleScene.play();*/
		}
		private function playAction(evt:Event):void{
			var battler:Battler=battleModel.activeBattler;
			
			var targets:Array=battler.action.actTargets
			if (targets&&targets.length>0){
				trace("战斗！"+battler.action.actTargets)
				var target:Battler=battler.action.actTargets[0]
				var unitView:UnitView=mapView.getUnit(battler);
				var atkDir:int=5
				if (target.x-battler.x==1){
					atkDir=6
				}else if (target.x-battler.x==-1){
					atkDir=4
				}
				unitView.playAttack(atkDir);
				TweenLite.delayedCall(0.8,battleModel.process_action);
				//			
			}else{
				battleModel.process_action();
			}
			
			
		}
		
		private function clickBattleScene(evt:Event):void{
			/*warScene.battleScene.visible=false;
			mapView.endAction(mapView.curUnitSp);
			if (map.checkEndTurn()){
				map.nextTroop();
			}else if (map.turnSide>0){
				TweenLite.delayedCall(1,master.act2);
			}*/
		}
		
	}
}